package cn.lvhang.pet.controller;

import cn.lvhang.basic.util.AjaxResult;
import cn.lvhang.basic.util.LoginContext;
import cn.lvhang.basic.util.PageList;
import cn.lvhang.pet.domain.Pet;
import cn.lvhang.pet.query.PetQuery;
import cn.lvhang.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    //分页 + 高级查询   post
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query, HttpServletRequest request){
        System.out.println(request.getHeader("token"));
        return petService.queryPage(query);
    }



    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }
    }

    @GetMapping("/{id}")
    public Pet queryById(@PathVariable("id") Long id){
        return petService.queryById(id);
    }

    @GetMapping("/adopt/{id}")
    public AjaxResult adoptPet(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            petService.adoptPet(id, LoginContext.currentLoginInfo(request).getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试！");
        }
    }
}
